﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;
using HtmlAgilityPack;
using System.Xml.Linq;
using System.Device.Location;
using System.Xml;

namespace sodexo
{
    public class geoResult
    {
        public double lat;
        public double lng;
        public string address;
    }
    public partial class PageMap : PhoneApplicationPage
    {
        private string address;
        private string geocodingUriFormat;
        private EventHandler<HtmlDocumentLoadCompleted> webCallBack;
        private IList<geoResult> geoResults;
        private Pushpin mark;
        private MapLayer customLayer;
        public PageMap()
        {
            InitializeComponent();
            geocodingUriFormat = "https://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false&region=cn&language=zh-CN";
            webCallBack += ReadGeoXML;
            mark = new Pushpin();
            customLayer = new MapLayer();
            this.myMap.Children.Add(customLayer);
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            this.address = HttpUtility.UrlEncode(NavigationContext.QueryString["address"]);
            

        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            string webUri = ( string.Format(geocodingUriFormat, this.address));
            HtmlWeb.LoadAsync(webUri, this.webCallBack);
        }
        private void ReadGeoXML(object sender, HtmlDocumentLoadCompleted args)
        {
            if (args.Error == null)
            {
                XElement geoXmldoc = XElement.Parse(args.Document.DocumentNode.InnerHtml);
                string status = geoXmldoc.Element("status").Value;

                if (status != "OK")
                {
                    MessageBox.Show("找不到地址坐标!");
                }
                
                var geoPoints = from result in geoXmldoc.Descendants("result")
                                select new geoResult
                                {
                                    address = result.Element("formatted_address").Value,

                                    lat = Convert.ToDouble(result.Element("geometry").Element("location").Element("lat").Value),
                                    lng = Convert.ToDouble(result.Element("geometry").Element("location").Element("lng").Value),
                                };
                this.geoResults = geoPoints.ToList<geoResult>();

                

                GeoCoordinate coordinate = new GeoCoordinate(this.geoResults[0].lat, this.geoResults[0].lng);
                myMap.ZoomLevel = 17;
                myMap.Center = coordinate;
               // this.PushpinMark.Location = coordinate;
                try
                {
                    foreach (geoResult gr in this.geoResults)
                    {
                        Pushpin pin = new Pushpin();
                        pin.Content = gr.address;
                        pin.Location = new GeoCoordinate(gr.lat,gr.lng);
                        pin.Background = new SolidColorBrush(Colors.Blue);
                        this.myMap.Children.Add(pin);
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
              
            }
        }

    }
    public class BingTileSource : TileSource
    {
        public BingTileSource()
            : base()
        {

        }
        public override Uri GetUri(int x, int y, int zoomLevel)
        {
            string quadkey = new QuadKey(x, y, zoomLevel).Key;
            string urlString = "http://r3.tiles.ditu.live.com/tiles/r" + quadkey + ".png?g=47";
            return new Uri(urlString);
        }
    }
}